package com.androturka;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Handler;
import android.util.Log;

public class WebAdapter implements IDataAdapter {

	private static final String LOGTAG = "Deprem";

	//private static final String NAMESPACE = "http://10.0.2.2/deprem";
	//private static final String serverURL = "http://10.0.2.2/deprem/server.php"; //

	//public static final String NAMESPACE = "http://192.168.2.167/deprem";
	//private static final String serverURL = "http://192.168.2.167/deprem/server.php";

	//private static final String NAMESPACE = "http://www.mekya.com/androturka";
	//private static final String serverURL = "http://www.mekya.com/androturka/server.php";
	
	static final String NAMESPACE = "http://depremgunlugu.com";
	private static final String serverURL = "http://depremgunlugu.com/server.php";
	

	@Override
	public void getEarthQuakeList(final String METHOD_NAME,final String paramName, final String paramValue, 
			final IArrayListDataListener dataListener) {
		Thread thread = new Thread() {
			Handler handler = new Handler();
			ArrayList<Object> elements = new ArrayList<Object>();
			@Override
			public void run() {
				//final String METHOD_NAME = "getAfterEarthQuakes";
				final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				request.addProperty(paramName, paramValue);

				
				//final String METHOD_NAME = "getEarthQuakes";
				//final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
				//SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				//request.addProperty("count", count);
				
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);
				HttpTransportSE androidHttpTransport;
				try {
					androidHttpTransport = new HttpTransportSE(serverURL);
					androidHttpTransport.debug = true;

					androidHttpTransport.call(SOAP_ACTION, envelope);

					@SuppressWarnings("unchecked")
					Vector<SoapObject> getElementsResult = (Vector<SoapObject>) envelope.getResponse();
					int earthquakeCount;
					earthquakeCount = getElementsResult.size();
					Log.v(LOGTAG, "Number of Earthquake Found: "
							+ earthquakeCount);

					for (int i = 0; i < earthquakeCount; i++) {
						SoapObject element = getElementsResult
								.get(i);
						elements.add(getEarthQuakeObject(element));
					}

				} catch (Exception E) {
					Log.i("e---xception", E.getMessage());
				}

				if (dataListener != null) {

					handler.post(new Runnable() {
						@Override
						public void run() {
							dataListener.dataArrived(elements);
						}
					});
				}
			}
		};
		thread.start();
	}
	
	
	
	private EarthQuake getEarthQuakeObject(SoapObject object) {
		String DATE_STR = "date";
		String LATITUDE_STR = "latitude";
		String LONGITUDE_STR = "longitude";
		String DEPTH_STR = "depth";
		String MAGNITUDE_STR = "magnitude";
		String LOCATION_STR = "location";

		String date = (String) object.getProperty(DATE_STR);
		Double latitude = Double.parseDouble(object.getProperty(LATITUDE_STR)
				.toString());
		Double longitude = Double.parseDouble(object.getProperty(LONGITUDE_STR)
				.toString());
		Double depth = Double.parseDouble(object.getProperty(DEPTH_STR)
				.toString());
		Double magnitude = Double.parseDouble(object.getProperty(MAGNITUDE_STR)
				.toString());
		String location = (String) object.getProperty(LOCATION_STR);

//		Log.v(LOGTAG, "Earhquake " + "Date: " + date + " Latitude: " + latitude
//				+ " Longitude: " + longitude + " Depth: " + depth
//				+ " Magnitude: " + magnitude + " Location: " + location);

		
		return new EarthQuake(getDate(date), latitude, longitude, depth, magnitude,
				location);
	}
	
	static Date getDate( String dateStr )
	{		
		//parse dateStr and returns date
		//20111203022703
		//2011 12 03 02 27 03
		int year 	= 0;
		int month 	= 0;
		int day 	= 0;
		int hour 	= 0;
		int minute 	= 0;
		int second 	= 0;
		
		year = Integer.parseInt(dateStr.substring(0, 4));
		month = Integer.parseInt(dateStr.substring(4, 6));
		day = Integer.parseInt(dateStr.substring(6, 8));
		hour = Integer.parseInt(dateStr.substring(8, 10));
		minute = Integer.parseInt(dateStr.substring(10, 12));
		second = Integer.parseInt(dateStr.substring(12, 14));


		Date date = new Date(year-1900,month-1,day,hour,minute,second);

		return date;
	}

	String getDateStr( )
	{				
		Date date = new Date();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String formattedDate = df.format(date);
		//Log.v(LOGTAG, formattedDate);
		return formattedDate;
	}

}
